package com.ynu.znkz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ynu.znkz.entity.PaperResearchMethod;
import com.ynu.znkz.entity.PaperResearchProblem;
import com.ynu.znkz.mapper.PaperResearchProblemMapper;
import com.ynu.znkz.service.PaperResearchProblemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lu
 * @since 2021-05-29
 */
@Service
public class PaperResearchProblemServiceImpl extends ServiceImpl<PaperResearchProblemMapper, PaperResearchProblem> implements PaperResearchProblemService {

    @Override
    public String savePaperResearchProblem(String paperResearchProblemName) {
        QueryWrapper<PaperResearchProblem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",paperResearchProblemName);
        List<PaperResearchProblem> paperResearchProblems = baseMapper.selectList(queryWrapper);

        //研究方法数据库中没有当前研究方法
        if(paperResearchProblems.isEmpty()) {
            PaperResearchProblem paperResearchProblem = new PaperResearchProblem();
            paperResearchProblem.setName(paperResearchProblemName);
            baseMapper.insert(paperResearchProblem);
            return paperResearchProblem.getId();
        }else
            return paperResearchProblems.get(0).getId();
    }
}
